/*
 * CargosAction.java
 *
 * Created on 10 de diciembre de 2005, 15:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.fiel.admin.struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.fiel.FielConstants;
import org.fiel.admin.dao.CargoDAO;
import org.fiel.admin.struts.forms.CargoForm;

/**
 * @author Luis Pablo Gallo (luispablo.gallo@gmail.com)
 */
public class CargosAction extends FielAction{

    public ActionForward list   ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        try{
            CargoDAO dao = new CargoDAO();
            request.setAttribute(FielConstants.CARGOS, dao.retrieve());
        }catch (Exception ex){
            ex.printStackTrace();
            throw ex;
        }
        
        return mapping.findForward(this.authorize(request, FielConstants.SHOW_LIST));
    }
    
    public ActionForward add( ActionMapping mapping
                            , ActionForm form
                            , HttpServletRequest request
                            , HttpServletResponse response )
    throws Exception{
        request.setAttribute(FielConstants.EDITABLE, FielConstants.TRUE);
        return mapping.findForward(this.authorize(request, FielConstants.ADD));
    }

    public ActionForward save   ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        CargoDAO dao = new CargoDAO();

        if( ((CargoForm)form).getId()==null || ((CargoForm)form).getId().trim().length()<=0 ){
            dao.create((CargoForm)form);
        }else{
            dao.update((CargoForm)form);
        }

        return this.list(mapping, form, request, response);
    }

    public ActionForward view   ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        CargoDAO dao = new CargoDAO();
        CargoForm cargo = dao.retrieve(request.getParameter("id"));
        
        ((CargoForm)form).setId(cargo.getId());
        ((CargoForm)form).setNombre(cargo.getNombre());
        ((CargoForm)form).setOrden(cargo.getOrden());
        
        request.setAttribute( FielConstants.EDITABLE
                            , request.getParameter(FielConstants.EDITABLE) );

        return mapping.findForward(this.authorize(request, FielConstants.ADD));
    }

    public ActionForward delete ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        CargoDAO dao = new CargoDAO();
        dao.delete(request.getParameter("id"));
        
        return this.list(mapping, form, request, response);
    }

}
